#!/bin/bash
version=$(date -d "8 hour" -u +%y.%m.%d.%H)
cd ~/app-root/data/github-sh/xpi/pzb
latest=$(curl -s https://addons.mozilla.org/zh-CN/firefox/addon/puzzle-toolbars/versions/ |grep '\.xpi'|awk -F '\"' '{print $2}'|sed '2,$d')
if ! grep -q "$latest" ~/app-root/data/temp/xpi/pzb/latest.txt; then
echo "$latest" > ~/app-root/data/temp/xpi/pzb/latest.txt
wget ${latest} --output-document=pzb-${version}.xpi
unzip -o pzb-${version}.xpi chrome/content/paneURLBar.xul chrome/content/puzzleBars.xul chrome/content/statusBar.xul chrome/content/urlbar* -d do/
unzip -o pzb-${version}.xpi chrome/skin/common/defaults.css chrome/skin/common/icon.png chrome/skin/common/puzzleBars.css chrome/skin/common/statusBar.css chrome/skin/common/urlbar.css -d do/
unzip -o pzb-${version}.xpi chrome/skin/linux/puzzleBars.css chrome/skin/linux/statusBar.css chrome/skin/linux/urlbar.css -d do/
unzip -o pzb-${version}.xpi chrome/skin/mac/puzzleBars.css chrome/skin/mac/statusBar.css chrome/skin/mac/urlbar.css -d do/
unzip -o pzb-${version}.xpi chrome/skin/win/puzzleBars.css chrome/skin/win/statusBar.css chrome/skin/win/urlbar.css -d do/
unzip -o pzb-${version}.xpi resource/defaults.js chrome/locale/en-US/overlay.dtd chrome/locale/zh-CN/overlay.dtd resource/modules/utils/PrefPanes.jsm -d do/
zip -d -r pzb-${version}.xpi LICENSE META-INF/* chrome/*  resource/modules/compatibilityFix/* resource/changelog.json resource/modules/bottom.jsm resource/modules/corner.jsm resource/modules/lateral.jsm resource/modules/placePP.jsm
zip -d pzb-${version}.xpi resource/modules/specialWidgets.jsm resource/modules/top.jsm resource/modules/utils/about.jsm resource/modules/utils/content.js resource/modules/utils/DnDprefs.jsm resource/modules/utils/PrivateBrowsing.jsm
zip -d pzb-${version}.xpi resource/modules/utils/HTMLElements.jsm resource/modules/utils/keydownPanel.jsm resource/modules/utils/Messenger.jsm resource/modules/utils/preferencesUtils.jsm resource/modules/utils/toCode.jsm resource/modules/utils/windowTools.jsm
mv pzb-${version}.xpi do/pzb-${version}.xpi
cd do/
sed -i -e '/addonUUID/d' -e '/bottom_bar/,/corner_alt/d' -e '/lateral_bar/,/top_alt/d' -e '/paneBottom/,/paneTop/d' -e '/homepage/,/development/d' resource/defaults.js
sed -i -e '/urlbar_pp/s#true#false#'  -e '/urlbar_autohide/s#true#false#' resource/defaults.js
sed -i -e '/AddonData\.version\.includes/,/AddonData\.version;/d' -e '/dev\ version/,+1d' resource/modules/utils/PrefPanes.jsm
sed -i "s#[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}#${version}#" install.rdf
zip -r pzb-${version}.xpi *
rm -rf chrome resource ~/app-root/repo/扩展/PuzzleBars2-"17\.[0-9]\{2\}\.[0-9]\{2\}\.[0-9]\{2\}".xpi
mv -f pzb-${version}.xpi ~/app-root/runtime/repo/扩展/PuzzleBars2-${version}.xpi
~/app-root/data/github-sh/log/log.sh "PuzzleBars2-${version}.xpi 更新"
fi
